﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Activation;
using System.ServiceModel;
using System.ServiceModel.Web;

using ALiteTeam.PackageTracer.Service;
using System.Runtime.Serialization.Json;
using System.IO;

namespace ALiteTeam.PackageTracer.Web
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class QueryAgent
    {
        public IExQueryHandler Handler { get; private set; }

        public QueryAgent()
        {
        }

        [WebGet(UriTemplate = "{name}")]
        public string Query(string name)
        {
            return string.Format("Test Suceed!{0}", name);
        }

        [WebGet(UriTemplate = "{op}/{flag}/{orderNo}", ResponseFormat = WebMessageFormat.Json)]
        public QueryResult QueryDetails(string op, string flag, string orderNo)
        {
            QueryResult result = null;
            switch (op)
            {
                case "shunfeng":
                    {
                        this.Handler = new SfHandler();
                        this.Handler.Identitier = SfIdentifier.Instance;
                        this.Handler.Filter = new SfFilter();
                        
                        break;
                    }
                case "ems":
                    {
                        this.Handler = new EmsHandler();
                        this.Handler.Identitier = EmsIdentifier.Instance;
                        this.Handler.Filter = new EmsFilter();

                        break;
                    }
            }

            if (!string.IsNullOrEmpty(flag) && flag == "1")
            {
                result = this.Handler.QueryDetails(orderNo);
            }
            else
            {
                result = this.Handler.QuerySummary(orderNo);
            }

            return result;
        }
    }
}